import * as Koa from 'koa';

const app = new Koa();
const json = require('koa-json');
const onerror = require('koa-onerror');
const logger = require('koa-logger');

const bodyParser = require('koa-bodyparser');
const session = require('koa-session');
const convert = require('koa-convert');
const serve = require('koa-static');
import routeList from './controller'
import env from './config/env'
const favicon = require('koa-favicon')
const nunjucks = require('koa-nunjucks-2')
const path = require('path');
onerror(app);

const isDev = process.env.NODE_ENV === 'dev';

app.use(json());
app.use(logger());
app.use(bodyParser());
app.use(convert(session(app)));
app.use(serve(path.join(__dirname, 'public')));

app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
// add nunjucks as view:
app.use(nunjucks({
  ext: 'html',
  path: path.join(__dirname, 'views'),
  nunjucksConfig: {
    noCache: isDev,
    watch: isDev,
    trimBlocks: true
  }
}));

//加载路由
routeList().forEach(dir => {
  let route = require(dir);
  app.use(route.default.routes());
});

export default app;
